import {page, pageFunc} from "../common/PageBuilder";
import {ItemDetailPage} from "../common/pages/ItemDetailPage";
import {BaseData, MainData} from "../../modules/coreModule/data/BaseData";
import {BasePageData} from "../common/core/BasePage";
import {field} from "../../modules/coreModule/data/DataLoader";
import {CloudFileUtils, MediaFile} from "../../utils/CloudFileUtils";
import CustomEvent = WechatMiniprogram.CustomEvent;
import {UserPage} from "../common/partPages/UserPage";
import {alertMgr, handleError} from "../../modules/coreModule/managers/AlertManager";
import {UploaderPage} from "../common/partPages/UploaderPage";
import {ReportInterface} from "../../modules/reportModule/ReportCommon";
import {Report} from "../../modules/reportModule/data/Report";

class Data extends BasePageData{
    @field(Report)
    item: Report;

    // 可选举报类型
    @field([String])
     optionalType: string[] = [];

    // 截图证明
    @field([Object])
    images: MediaFile[] = [];
}

class Param extends BaseData {
    @field(String)
    taskId: string;
}

@page("report","匿名举报")
export class ReportPage extends ItemDetailPage<Data, Report, Param>{

    public data = new Data();

    /**
     * 部分页
     */
    public userPage: UserPage = new UserPage();
    public uploaderPage: UploaderPage = new UploaderPage("report","report");

    /**
     * 初始化
     */
    onLoad(e) {
        super.onLoad(e);
        this.setItem(new Report()).then();
        this.setData({
            optionalType: ['反动政治', '色情低俗', '涉嫌诈骗',
                '广告引流', '骚扰辱骂', '血腥暴力',
                '违法行为', '未成年', '其他内容']
        });
    }

    // region 输 入字段交互

    // 更新补充信息
    @pageFunc
    public onSupplementChange(e: CustomEvent<any>) {
        //bind:blur事件的返回值是 event.detail = {value,cursor}
        this.setItemData({supplement: e.detail.value}).then();
    }

    // 更新类型选择信息
    @pageFunc
    public select(e: CustomEvent<any>) {
        const types = this.item.types;
        const selectedType = e.currentTarget.dataset.msg;
        if(!types.includes(selectedType)){
            types.push(selectedType);
            console.log(selectedType);
        }else{
            let index = types.indexOf(selectedType);
            types.splice(index,1);
        }
        this.setItemData({types}).then();
    }

    // endregion

    /**
     * 提交
     */
    @pageFunc
    @handleError
    public async submit(){
        await this.uploadPictures();
        this.check();
        this.item.taskId = this.params.taskId;
        await ReportInterface.PublishReport({
            report: this.item
        });
        // TODO:同步item数据
        if(await alertMgr().showAlert("举报信息已提交！")) this.back();
    }

    /**
     * 校验格式
     */
    private check(){
        if(this.item.types.length==0) throw "请选择举报类型";
        if(this.item.images.length==0) throw "请上传相关截图";
    }

    /**
     * 上传图片
     */
    private async uploadPictures() {
        const fileIds = await this.uploaderPage.uploadPictures();
        await this.setItemData({images: fileIds});
    }

}
